package com.example.bootdemo.controller;

import com.example.bootdemo.bean.Schools;
import com.example.bootdemo.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.example.bootdemo.BootdemoApplication.*;

@Controller
@RestController
@CrossOrigin()
public class RollStudentsController {


    @RequestMapping("/outputStudentData")
    @ResponseBody
    public List<Student> outputStudentData(@RequestBody Map<String,Object> school){
//        System.out.println(school);
//        System.out.println("school:"+school.get("schoolName"));
//        List<Student> students = new ArrayList<>();
//        students =Tool.initStudentData(60);
        return map.get(school.get("schoolName"));
    }
    //向前台传递所有学校的信息
    @RequestMapping("/viewSchoolList")
    @ResponseBody
    public List<Schools> viewSchoolList(){
        List<Schools> schoolsList=new ArrayList<>();
        for(String str : schools){
            Schools school = new Schools();
            school.setValue(str);
            school.setText(str);
            schoolsList.add(school);
        }
        return schoolsList;
    }
    //向前台传递该学校已抽取多少人的信息
    @RequestMapping("viewCountNumb")
    @ResponseBody
    public int viewCountNumb(@RequestBody String schoolName){
//        System.out.println(schoolName);
        if(!selectedStudent.containsKey(schoolName)){
            return 0;
        }else {
            return selectedStudent.get(schoolName).size();
        }
    }
    //接收前台抽到学生的信息并向前台传递该学校已抽取多少人的信息
    @RequestMapping("deleteSelectedStudent")
    @ResponseBody
    public int deleteSelectedStudent(@RequestBody List<Student> studentList){
        //显示抽到的人信息
//        for (Student student:studentList){
//            System.out.println(student.getStudentNumb());
//            System.out.println(student.getStudentName());
//        }
        //将抽到的学生信息加入到selectedStudent的Map集合中
        if(!selectedStudent.containsKey(studentList.get(0).getChoiceSchool())){
            //如果没有该学校则在map中添加该学校对应的key，并将抽取到的学生集合添加到对应key的value
            //System.out.println("没有该学校");
            selectedStudent.put(studentList.get(0).getChoiceSchool(),studentList);
        }else {
            //逐一将抽到的学生信息添加到已存在的key对应的value集合中
            for(Student student:studentList){
                selectedStudent.get(studentList.get(0).getChoiceSchool()).add(student);
            }
        }
        //将抽到的学生从所有分好类的学生信息map中剔除
        for(int i=0;i<map.get(studentList.get(0).getChoiceSchool()).size();i++){
            for(int j=0;j<studentList.size();j++){
                if(map.get(studentList.get(0).getChoiceSchool()).size()==0){
                    break;
                }
                if(map.get(studentList.get(0).getChoiceSchool()).get(i).getStudentNumb().equals(studentList.get(j).getStudentNumb())){
                    if(i==0){
                        map.get(studentList.get(0).getChoiceSchool()).remove(i);
                    }else {
                        map.get(studentList.get(0).getChoiceSchool()).remove(i--);
                    }
                }
            }
        }
        //打印剔除完后剩余学生
//        System.out.println("本次抽取后，剩余学生");
//        for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
//            //System.out.println(iterator.next());
//            for(Student student:entry.getValue()){
//                System.out.println("ID:"+Integer.parseInt(student.getStudentNumb())+"   "+"name:"+student.getStudentName()+"   "+"choiceSchool:"+student.getChoiceSchool());
//            }
//        }
        //打印抽取已抽取的学生的map集合中的信息
//        System.out.println("本次抽取后，已抽取的学生信息");
//        for (Map.Entry<String, List<Student>> entry : selectedStudent.entrySet()) {
//            //System.out.println(iterator.next());
//            for(Student student:entry.getValue()){
//                System.out.println("ID:"+Integer.parseInt(student.getStudentNumb())+"   "+"name:"+student.getStudentName()+"   "+"choiceSchool:"+student.getChoiceSchool());
//            }
//        }
        return selectedStudent.get(studentList.get(0).getChoiceSchool()).size();
    }
}
